﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace Thksoft.Module.BillManagement
{
    public partial class TradeBill : BasePage
    {
        private YX_TradeBillBLL bbbll = new YX_TradeBillBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDate.Value = ""; //DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");

                txtEnd.Value = ""; //DateTime.Now.ToString("yyyy-MM-dd");

                ViewState["SortOrder"] = "YXTB.N_TRADEBILL_ID";
                ViewState["OrderDire"] = " desc ";

                BindData(true);
            }
        }

        private void Bind()
        {
            string whereStr = "";
            //出票账号
            if (txtDRAWERACCOUNT.Text.Trim() != "")
            {
                whereStr += " AND YXTB.C_TRADEBILL_DRAWERACCOUNT LIKE '%" + txtDRAWERACCOUNT.Text.Trim() + "%'";
            }
            //出票公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " AND YXTB.N_TRADEBILL_DRAWERCMYID= " + FORM_COMPANY_ID.Value;
                /*whereStr += @"  and YXTB.N_TRADEBILL_DRAWERCMYID in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + FORM_COMPANY_ID.Value + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";*/
            }
            else
            {
                whereStr += @"  and YXTB.N_TRADEBILL_DRAWERCMYID in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            //收款公司
            if (FORM_PAYEEName.Text.Trim() != "")
            {
                whereStr += " AND YXTB.C_TRADEBILL_PAYEECMYNAME LIKE '%" + FORM_PAYEEName.Text.Trim() + "%'";
            }

            //票据日期
            if (txtDate.Value.Trim() != "" && txtEnd.Value.Trim() != "")
            {
                whereStr += @" and YXTB.D_TRADEBILL_STARTDATE between
       TO_DATE('" + txtDate.Value + @"', 'YYYY-MM-DD') and
       TO_DATE('" + txtEnd.Value + @"', 'YYYY-MM-DD') ";
            }

            //收款账号
            if (txtPAYEEACCOUNT.Text.Trim() != "")
            {
                whereStr += " AND YXTB.C_TRADEBILL_PAYEEACCOUNT LIKE '%" + txtPAYEEACCOUNT.Text.Trim() + "%'";
            }
            ViewState["Where"] = whereStr;

            BindData(true);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //点击排序
            string sPage = e.SortExpression;

            if (sPage == "")
            {
                return;
            }
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
                ViewState["OrderDire"] = "ASC";
            }

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = " AND YXTB.N_TRADEBILL_DRAWERCMYID= " + FORM_COMPANY_ID.Value;
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = bbbll.GetRecordCount(Where);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = bbbll.GetListByPage(Where, sort, PageIng.CurrentPage, PageIng.RowsPerPage);

            var row = dt.NewRow();
            row["N_TRADEBILL_ID"] = DBNull.Value;
            row["CMY_NAME"] = "合计";
            row["C_TRADEBILL_DRAWERACCOUNT"] = DBNull.Value;
            row["C_TRADEBILL_PAYEECMYNAME"] = DBNull.Value;
            row["C_TRADEBILL_PAYEEACCOUNT"] = DBNull.Value;
            row["N_TRADEBILL_INVOICEVALUE"] = dt.AsEnumerable().Sum(a => Convert.ToDecimal(a["N_TRADEBILL_INVOICEVALUE"]));
            row["D_TRADEBILL_STARTDATE"] = DBNull.Value;
            row["D_TRADEBILL_ENDDATE"] = DBNull.Value;
            row["C_TRADEBILL_REMARK"] = DBNull.Value;
            dt.Rows.Add(row);

            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                grvShow1.DataSource = null;
                grvShow1.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow1.Rows.Count; i++)
            {
                if (((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).ToolTip;
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaoChu_Click(object sender, EventArgs e)
        {
            var dt = (DataTable)ViewState["database"];
            dt.Columns["CMY_NAME"].ColumnName = "汇款公司名称";
            dt.Columns["C_TRADEBILL_DRAWERACCOUNT"].ColumnName = "汇款账号";
            dt.Columns["C_NAME"].ColumnName = "汇款银行";
            dt.Columns["C_TRADEBILL_PAYEECMYNAME"].ColumnName = "收款公司名称";
            dt.Columns["C_TRADEBILL_PAYEEACCOUNT"].ColumnName = "收款账号";
            dt.Columns["C_TRADEBILL_PAYEEBANKNAME"].ColumnName = "收款银行";

            dt.Columns["USER_NAME"].ColumnName = "出票人";
            dt.Columns["C_TRADEBILL_PAYERNAME"].ColumnName = "付款人";
            dt.Columns["C_TRADEBILL_PAYEENAME"].ColumnName = "收款人";

            dt.Columns["N_TRADEBILL_INVOICEVALUE"].ColumnName = "汇款金额";
            dt.Columns["D_TRADEBILL_STARTDATE"].ColumnName = "出票日期";
            dt.Columns["D_TRADEBILL_ENDDATE"].ColumnName = "最后日期";
            dt.Columns["C_TRADEBILL_CODE"].ColumnName = "交易合同号";
            dt.Columns["N_TRADEBILL_IFOFFER"].ColumnName = "能否转让";
            dt.Columns["C_TRADEBILL_REMARK"].ColumnName = "承兑信息";
            dt.Columns.Remove("N_TRADEBILL_ID");
            dt.Columns.Remove("r");
            toExecl1(dt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "银行汇票");
        }
    }
}